﻿using AiXiu.Common;
using AiXiu.IService;
using AiXiu.Model;
using System;
using System.Data.Entity;
using System.Linq;

namespace AiXiu.Service
{
    public class UserService : IUserService
    {
        Model1 model = new Model1();
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        public OperResult Register(TBLogins login)
        {
            if (string.IsNullOrWhiteSpace(login.UserName))
            {
                return OperResult.Failed("用户名不能为空！");
            }
            if (string.IsNullOrWhiteSpace(login.MobileNumber))
            {
                return OperResult.Failed("手机号不能为空！");
            }
            if (string.IsNullOrWhiteSpace(login.Password))
            {
                return OperResult.Failed("密码不能为空！");
            }
            // 判断用户名是否已注册
            if (model.TBLogins.Any(m => m.UserName == login.UserName))
            {
                return OperResult.Failed("用户名已注册！");
            }
            // 判断手机号是否已注册
            if (model.TBLogins.Any(m => m.MobileNumber == login.MobileNumber))
            {
                return OperResult.Failed("手机号已注册！");
            }
            
            // 加密用户密码
            SHAEncryption shaEncryption = new SHAEncryption();
            login.Password = shaEncryption.SHA1Encrypt(login.Password);
            model.TBLogins.Add(login);
            int n = model.SaveChanges();
            if (n > 0)
            {
                TBUsers user = new TBUsers();
                user.Id = login.Id;
                user.NickName = $"用户{user.Id}";
                user.Sex = 0;
                user.CreationTime = DateTime.Now;
                model.TBUsers.Add(user);
                model.SaveChanges();
            }
            return OperResult.Succeed();
        }

        /// <summary>
        /// 通过用户名登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public OperResult<TBUsers> LoginByUserName(string userName,string password)
        {
            //判断用户名和密码是否为空
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                return OperResult<TBUsers>.Failed("用户名或密码不能为空！");
            }

            //通过用户名查找用户,判断用户是否存在
            TBLogins loginEntity = model.TBLogins.SingleOrDefault(a=>a.UserName==userName);
            if (loginEntity == null)
            {
                return OperResult<TBUsers>.Failed("该用户不存在");
            }

            //验证用户的密码是否正确
            SHAEncryption sHAEncryption = new SHAEncryption();
            //currentPwd 当前页面输入的密码进行加密
            string currentPwd = sHAEncryption.SHA1Encrypt(password);
            if (currentPwd!=loginEntity.Password)
            {
                return OperResult<TBUsers>.Failed("密码错误！");
            }

            //查找用户详细信息
            TBUsers user = model.TBUsers.Find(loginEntity.Id);
            return OperResult<TBUsers>.Succeed(user);
        }

        /// <summary>
        /// 通过手机号登录
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public OperResult<TBUsers> LoginByMobile(string mobile, string password)
        {
            //判断手机号和密码是否为空
            if (string.IsNullOrEmpty(mobile) || string.IsNullOrEmpty(password))
            {
                return OperResult<TBUsers>.Failed("用户名或密码不能为空！");
            }

            //通过手机号查找用户,判断用户是否存在
            TBLogins loginEntity = model.TBLogins.SingleOrDefault(a => a.MobileNumber == mobile);
            if (loginEntity == null)
            {
                return OperResult<TBUsers>.Failed("该手机号不存在");
            }

            //验证用户的密码是否正确
            SHAEncryption sHAEncryption = new SHAEncryption();
            //currentPwd 当前页面输入的密码进行加密
            string currentPwd = sHAEncryption.SHA1Encrypt(password);
            if (currentPwd != loginEntity.Password)
            {
                return OperResult<TBUsers>.Failed("密码错误！");
            }

            //查找用户详细信息
            TBUsers user = model.TBUsers.Find(loginEntity.Id);
            return OperResult<TBUsers>.Succeed(user);
        }

        /// <summary>
        /// 通过用户编号查找用户的详细信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public TBUsers FindByUserId(int userId)
        {
            return model.TBUsers.Find(userId);
        }

        /// <summary>
        /// 更新用户信息(不包含头像)
        /// </summary>
        /// <param name="user">用户信息</param>
        /// <returns></returns>
        public OperResult<TBUsers> EditUser(TBUsers user)
        {
            try
            {
                // 更新到数据库
                model.Entry(user).State = EntityState.Modified;
                int n=model.SaveChanges();
                if (n > 0)
                {
                    return OperResult<TBUsers>.Succeed(user);
                }
                else
                {
                    return OperResult<TBUsers>.Failed("信息更新失败！");
                }
            }
            catch (Exception ex)
            {
                return OperResult<TBUsers>.Failed("信息更新失败！"+ex);
            }

        }

        /// <summary>
        /// 编辑头像
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public OperResult<TBUsers> EditAvatar(TBUsers user)
        {
            try
            {
                // 更新到数据库
                model.Entry(user).State = EntityState.Modified;
                int n = model.SaveChanges();
                if (n > 0)
                {
                    return OperResult<TBUsers>.Succeed(user);
                }
                else
                {
                    return OperResult<TBUsers>.Failed("信息更新失败！");
                }
            }
            catch (Exception ex)
            {
                return OperResult<TBUsers>.Failed("信息更新失败！" + ex);
            }            
        }
    }
}